package com.aha.dataanalyzeandview.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/******************************
 * @Project: coder-helper
 * @FileName: null.java
 * @ClassName: DateUtils
 * @time 2020/4/8 23:50
 * @version 1.00
 * @author nulijiushimeili
 * @Description:   日期格式化与解析标准日期
 ******************************/
@Slf4j
public class DateUtils {


    /**
     * 将时间转换为标准格式
     * @param date
     */
    public static String toSTDDateTime(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 将字符串日期格式转化为 日期 或者 日期时间
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public static Date parseStringToDate(String dateStr) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        if(dateStr.length()>10) {
            return sdf.parse(dateStr);
        }else{
            return sdf1.parse(dateStr);
        }
    }



    /**
     * 获取 N 天前的日期
     * @return
     */
    public static   String getLastNDayDateStr(int n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE,-n);
        Date d = c.getTime();
        String day = sdf.format(d);

        return day;
    }


    /**
     * 获取 N 天前的日期
     * @return
     */
    public static Date getLastNDayDate(int n){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.DATE,-n);
        Date d = c.getTime();
        return d;
    }

    /**
     * 1,2,3,4,5,6,0   转换为 MON、TUE、WED、THU、FRI、SAT、SUN
     * @return
     */
    public static String weekdays2StrWeekdays(String weekdays){
        String strWeekdays = "";
        for (String s : weekdays.split(",")) {
            if(s!=null && !"".equalsIgnoreCase(s)){
                int weekday = Integer.valueOf(s);
                switch (weekday){
                    case 1:
                        strWeekdays  = strWeekdays + "MON,";
                        break;
                    case 2:
                        strWeekdays  = strWeekdays + "TUE,";
                        break;
                    case 3:
                        strWeekdays  = strWeekdays + "WED,";
                        break;
                    case 4:
                        strWeekdays  = strWeekdays + "THU,";
                        break;
                    case 5:
                        strWeekdays  = strWeekdays + "FRI,";
                        break;
                    case 6:
                        strWeekdays  = strWeekdays + "SAT,";
                        break;
                    case 0:
                        strWeekdays  = strWeekdays + "SUN,";
                        break;
                }
            }

        }
        strWeekdays = strWeekdays.substring(0,strWeekdays.length()-1);

        return strWeekdays;
    }

}
